﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using MaxLeaf.Platform.DALImplementation;
using MaxLeaf.Platform.Functions;
using CommonsLib.Utility;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.Contract;
using System.Transactions;
using CommonsLib.Settings;

namespace MaxLeaf.Platform.BLLImplementation
{
    public class CompanyProcessor :   ICompanyProcess
    {
         
        public bool InitData(int intFromCompanyID, int intToCompanyID)
        {
            return new mCompanyBLL().InitData(intFromCompanyID, intToCompanyID);
        }


        public string GetUserSN(bool blnDirectCity, int intProvID, int intCityID, int intRegionID)
        {
            return new mCompanyBLL().GetUserSN(blnDirectCity, intProvID,intCityID,intRegionID);
        }

        public int AddCompany(mCompanyDataModel company, mCompany_InfoDataModel company_info,List<SCPCategoryDataModel> lstSCP, mUser_MainDataModel user, mUser_InfoDataModel user_info)
        {
            mCompanyBLL bllCompany = (mCompanyBLL)BaseBLL.GetDataModelBLL("mCompanyDataModel");
            BaseBLL bllCompany_info = BaseBLL.GetDataModelBLL("mCompany_InfoDataModel");
 

            int intCompanyKey = -1;
            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
               
                    try
                    {

                        company.Key = 0;
                        company = (mCompanyDataModel)bllCompany.Add(company);
                        intCompanyKey = company.Key;

                        company_info.Key = company.Key;
                        company_info = (mCompany_InfoDataModel)bllCompany_info.Add(company_info);

                        SCPCategoryBLL bllSCP = (SCPCategoryBLL)BaseBLL.GetDataModelBLL("SCPCategoryDataModel");
                        foreach (SCPCategoryDataModel topscp in bllSCP.SelectByCond(new SCPCategoryDataModel()
                                , new List<AddInfo>() { 
                                            new AddInfo{ Name= AddInfoType.Cond ,Value= new List<Cond>() { 
                                                    new Cond { PropName ="CompanyID", Compare = CompareType.Equal, Value =  company.ParentID }
                                                    ,new Cond { PropName ="ParentSCPCategorySeqNo", Compare = CompareType.Equal, Value =0 }
                                                }
                                            } 
                                        }
                            ).Data)
                        {
                            topscp.Is_Display = false;
                            foreach (SCPCategoryDataModel scp in lstSCP)
                            {
                                if (scp.SCPCategorySeqNo == topscp.SCPCategorySeqNo)
                                {
                                    topscp.Is_Display = scp.Is_Display;
                                    break;
                                }
                            }
                            topscp.CompanyID = company.Key;
                            topscp.Key = 0;
                            //LogManager.Debug("topscp.CompanyID " + topscp.CompanyID);
                            bllSCP.Add(topscp);
                            AddSCPChild(bllSCP, company.ParentID, topscp.CompanyID, topscp.SCPCategorySeqNo);
                        }

                        user.CompanyID = company.Key;
                        user.Is_CompanyUser = true;
                        user_info.CompanyID = company.Key;
                        AddUser(user, user_info);

                        InitData(company.ParentID, company.Key);
                        
                    }
                    catch (Exception ex)
                    {
                        LogManager.Debug(ex.Message);

                        intCompanyKey = -1;

                        throw;
                    }


                 
                ts.Complete();


                
            }
            
            using (DBAccess dbcon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase)))
            {
                try
                {
                    bllCompany.Update_Web(dbcon, company);
                }
                catch (Exception ex)
                {
                    LogManager.Error(ex);
                }
            }
            return intCompanyKey;
        }

        private void AddSCPChild(SCPCategoryBLL bllSCP, int intFromCompanyID, int intToCompanyID, int intParentSCPCategorySeqNo)
        {
            if(intParentSCPCategorySeqNo==0) return;

            foreach(SCPCategoryDataModel scp in bllSCP.SelectByCond(new SCPCategoryDataModel(),new List<AddInfo>(){
                new AddInfo( ){
                     Name= AddInfoType.Cond, Value =new List<Cond>()
                     {
                         new Cond(){ PropName="CompanyId", Compare = CompareType.Equal , Value=intFromCompanyID}
                         ,new Cond(){ PropName="ParentSCPCategorySeqNo", Compare = CompareType.Equal , Value=intParentSCPCategorySeqNo}
                     }
                }
            }).Data)
            {
                scp.CompanyID = intToCompanyID;
                scp.Key = 0;
                bllSCP.Add(scp);

                AddSCPChild(bllSCP, intFromCompanyID, intToCompanyID, scp.SCPCategorySeqNo);
            }
        }

      

        public int AddUser(mUser_MainDataModel User, mUser_InfoDataModel User_info)
        {
            int intUserKey = -1;
            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {

                try
                {
                    
                    mUser_MainBLL bllUser = (mUser_MainBLL)BaseBLL.GetDataModelBLL("mUser_MainDataModel");
                    BaseBLL bllUser_info = BaseBLL.GetDataModelBLL("mUser_InfoDataModel");

                    User.Key = 0;
                    User = (mUser_MainDataModel)bllUser.Add(User);
                    intUserKey = User.Key;

                    User_info.Key = User.Key;
                    User_info = (mUser_InfoDataModel)bllUser_info.Add(User_info);

                   
                }
                catch (Exception ex)
                {
                    LogManager.Debug(ex.Message);

                    intUserKey = -1;

                    throw;
                }



                ts.Complete();


                return intUserKey;
            }
        }
    }
}
